package com.nd.qa.shareplate.script.appium;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

import com.nd.qa.shareplate.model.Module;
import com.nd.qa.shareplate.model.PageElement;
import com.nd.qa.shareplate.model.Project;
import com.nd.qa.shareplate.script.junit.JavaJunitScript;
import com.nd.qa.shareplate.util.FileUtil;

/**
 * robotium脚本管理类
 * @version 1.00 wuqiaomin 新建
 *
 */
public class AppiumScriptTool {
	OutputStreamWriter java = null;
	StringBuilder content = null;
	Module program = null;
	String modelDirPath = null;
	String packageName = null;
	String activityName = null;
	//Map<Integer, String> operational = null;
	JavaJunitScript junitScript = new JavaJunitScript();
	Appium appium = new Appium();
	Project project = null;
	FileUtil fileUtil = new FileUtil();
	
	public AppiumScriptTool(Module program,Project project, String projectDirPath) {
		this.program = program;
		this.project = project;
		//this.operational = Util.dao.getOperational();
		this.modelDirPath = projectDirPath;
	}
	
	private void setComponent(){
		this.packageName = this.project.getProjectPackage();
		this.activityName = this.project.getProjectMainActivity();
	}
	
	/**
	 * 在本地创建脚本文件并初始化
	 * @param scriptName
	 */
	private void createScript(String scriptName){
		content = new StringBuilder();
	
		String path = this.packageName.replaceAll("\\.", "/");
		System.out.println("path:"+path);
		String fileName = this.modelDirPath+"src/test/java/"+path+"/test/" + scriptName+"Test";
	    File files =new File(fileName); 
	    //如果文件夹不存在则创建    
	    if  (!files .exists()  && !files .isDirectory()){       
	        System.out.println("//不存在");  
	        files.mkdirs();    
	    }else{  
	        System.out.println("//目录存在");  
	    }  
		File file = fileUtil.createFile(fileName, ".java");
		try {
			java = new OutputStreamWriter(
					new FileOutputStream(file), "UTF-8");
			java.write(initClass(this.program.getModuleName(),file.getName().split("\\.")[0],packageName, activityName));

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * appium脚本的固定模板
	 * @param modelName
	 * @param scriptName
	 * @param packageName
	 * @param activityName
	 * @return
	 */
	private String initClass(String modelName, String scriptName,
			String packageName, String activityName) {
		// TODO Auto-generated method stub
		return appium.scriptInit(modelName, scriptName, packageName, activityName);
	}
	
	private String writeCode(PageElement element) {
		StringBuilder sb = new StringBuilder();
		String operate = element.getElementActionName();
		if (operate.contains("drag")) {
			sb.append(appium.drag(element));
			sb.append(appium.sleep(4000));
		}else {
			switch (operate) {
			case "click":
				sb.append(appium.click(element));
				sb.append(appium.sleep(4000));
				break;
			case "input":
				sb.append(appium.enter(element));
				sb.append(appium.sleep(4000));
			default:
				break;
			}
		}
		return sb.toString();
	}
	
	private void endWrite(){
		if (java != null) {
			try {
				java.write(content.toString());
				java.write("}");
				java.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * 写入脚本具体内容
	 * @param scriptList
	 */
	public void generateScript(List<List<PageElement>> scriptList) {
		// TODO Auto-generated method stub
		
		setComponent();
		createScript("MainActivity");
		try {
			for (List<PageElement> list : scriptList) {
				StringBuilder sb = new StringBuilder();
				PageElement element = list.get(0);
				String caseName = String.format("%s%s", element.getElementTypeName(), element.getElementId());
				System.out.println("-----------------------------------------");
				int size = list.size();
				for (int i = size - 1; i >= 0; i--) {
					sb.append(writeCode(list.get(i)));
					System.out.println(list.get(i).toString());
				}
				content.append(junitScript.testCase(caseName, sb.toString()));
				System.out.println("-----------------------------------------");
			}
			endWrite();
		} catch (Exception e) {
			endWrite();
			e.printStackTrace();
		}
	}
}
